package com.dl.exam.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dl.exam.entity.Page;
import com.dl.exam.entity.question.SingleQuestion;
import com.dl.exam.mapper.SingleMapper;
import com.dl.exam.service.SingleService;
import com.dl.exam.util.PageUtils;
import com.dl.exam.util.UUIDUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 单选题服务层
 * @author DL
 * @create 2022-01-24 19:43
 */
@Service
@Transactional
public class SingleServiceImpl implements SingleService {

    @Resource
    private SingleMapper singleMapper;

    @Override
    public boolean addQuestion(SingleQuestion singleQuestion) {
        singleQuestion.setQuestionId(UUIDUtils.getUUID10());
        return singleMapper.insertQuestion(singleQuestion) != 0;
    }

    @Override
    public boolean removeQuestion(String questionId) {
        return singleMapper.deleteQuestion(questionId) != 0;
    }

    @Override
    public boolean modifyQuestion(JSONObject questionForm) {
        return singleMapper.updateQuestion(questionForm) != 0;
    }

    @Override
    public SingleQuestion getQuestionById(String questionId) {
        return singleMapper.queryQuestionById(questionId);
    }

    @Override
    public List<SingleQuestion> getUnfinishedQuestion(String examId) {
        return singleMapper.queryUnfinishedQuestion(examId);
    }

    @Override
    public List<SingleQuestion> getRandomQuestionByCondition(JSONObject jsonObject) {
        return singleMapper.queryRandomQuestionByCondition(jsonObject);
    }

    @Override
    public Page<SingleQuestion> getAllQuestion(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<SingleQuestion> list = singleMapper.queryAllQuestion();
        return (Page<SingleQuestion>) PageUtils.getPage(list);
    }

    @Override
    public Page<SingleQuestion> getQuestionByCondition(JSONObject condition) {
        Integer currentPage = condition.getInteger("currentPage");
        PageHelper.startPage(currentPage, 10);
        List<SingleQuestion> list = singleMapper.queryQuestionByCondition(condition);
        return (Page<SingleQuestion>) PageUtils.getPage(list);
    }

    @Override
    public Page<SingleQuestion> getFinishedExamAnswer(String examId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, 10);
        List<SingleQuestion> list = singleMapper.queryFinishedExamAnswer(examId);
        return (Page<SingleQuestion>) PageUtils.getPage(list);
    }

}
